﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DrillOnline.Services;
using DrillOnline.Services.Scenarios;


namespace DrillOnline.Controllers
{
    [Authorize]
    public class MediaController : _BaseController
    {
        //GET:			/Media/Video/id  e4a40b5f-b27c-4922-bb9c-23b10d22ec07 ，84b80ecb-9a7f-4944-a8f9-51c9e31738e3
        public ActionResult Index(string id)
        {
            var m = DB.DrillSubjectEvents.Find(Guid.Parse(id));
            return PartialView(m.AttachmentType, m);
        }


        //POST:	/Media/Login				groupAndUserList,login
        [AllowAnonymous]
        [HttpPost]
        public ActionResult Login(string user, string pwd)
        {
            var cs = GetCurrentSession();
            if (null == cs || !UserTenant.DrillSubjects.Any(s => s.ID == cs.Subject.ID)) { return Redirect("~/Account/LogOff"); }

            var u = cs.ActorGroups.FirstOrDefault(v => v.GroupKey=="[大屏幕]").Actors.FirstOrDefault(v => v.ActorKey.ToLower() == user.ToLower() && v.Password == pwd);
            if (null != u)
            {
                System.Web.Security.FormsAuthentication.SetAuthCookie(u.ActorKey, false);
                return Redirect("~/Media/CtrlPanel");
            }
            else
            {
                ModelState.AddModelError("loginfailed", new Services.SiteException("登录失败."));
                return Redirect("~/Account/Login");
            }
        }
        [HttpGet]
        public ActionResult CtrlPanel()
        {
            var cs = GetCurrentSession();
            if (null == cs || !UserTenant.DrillSubjects.Any(s => s.ID == cs.Subject.ID)) { return Redirect("~/Account/LogOff"); }

            var m = cs;
            return View(m);
        }
    }
}
